﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows;

namespace WokseleNaDrzewie
{
    public class Triangle
    {
        public Point3D[] points { get; set; }
        public Point3D center { get; set; }
        public Vector3D normal { get; set; }

        public Triangle(Point3D[] _points, Vector3D _normal)
        {
            points = _points;
            normal = _normal;
            center=new Point3D((points[0].X+points[1].X+points[2].X)/3, (points[0].Y+points[1].Y+points[2].Y)/3, (points[0].Z+points[1].Z+points[2].Z)/3);
        }

        public bool isInside(Point3D point)
        {
            Vector v1 = new Vector();
            Vector v = new Vector();
            sbyte[] a = new sbyte[3];
            sbyte sgn = 0;
            for (int i = 0; i < 3; i++)
            {
                v = new Vector(point.X - points[i].X, point.Y - points[i].Y);
                v1 = new Vector(points[(i + 1) % 3].X - points[i].X, points[(i + 1) % 3].Y - points[i].Y);
                a[i] = (sbyte)Math.Sign(Vector.AngleBetween(v1, v));
                if (sgn != a[i] && sgn != 0)
                    return false;
                //if (a[i] != 0)
                sgn = a[i];
            }
            return true;
        }
    }
}
